"""build_uuuid to uuid, job_uuid to uuid, job_name to name, run_uuid to
uuid

Revision ID: ba1fc5af0aed
Revises: 1cbf2e36f7fa
Create Date: 2021-01-27 09:50:23.478611

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "ba1fc5af0aed"
down_revision = "1cbf2e36f7fa"
branch_labels = None
depends_on = None


def upgrade():

    # EnvironmentBuild.build_uuid -> uuid
    op.alter_column("environment_build", "build_uuid", new_column_name="uuid")

    # Job.job_uuid -> uuid
    op.drop_constraint(
        "fk_pipeline_runs_job_uuid_jobs", "pipeline_runs", type_="foreignkey"
    )
    op.alter_column("jobs", "job_uuid", new_column_name="uuid")
    op.create_foreign_key(
        op.f("fk_pipeline_runs_job_uuid_jobs"),
        "pipeline_runs",
        "jobs",
        ["job_uuid"],
        ["uuid"],
        ondelete="CASCADE",
    )

    # Job.job_name -> name
    op.alter_column("jobs", "job_name", new_column_name="name")

    # PipelineRun.run_uuid -> uuid
    op.drop_constraint(
        "fk_pipeline_run_image_mappings_run_uuid_pipeline_runs",
        "pipeline_run_image_mappings",
        type_="foreignkey",
    )
    op.drop_constraint(
        "fk_pipeline_run_steps_run_uuid_pipeline_runs",
        "pipeline_run_steps",
        type_="foreignkey",
    )
    op.alter_column("pipeline_runs", "run_uuid", new_column_name="uuid")
    op.create_foreign_key(
        op.f("fk_pipeline_run_image_mappings_run_uuid_pipeline_runs"),
        "pipeline_run_image_mappings",
        "pipeline_runs",
        ["run_uuid"],
        ["uuid"],
        ondelete="CASCADE",
    )
    op.create_foreign_key(
        op.f("fk_pipeline_run_steps_run_uuid_pipeline_runs"),
        "pipeline_run_steps",
        "pipeline_runs",
        ["run_uuid"],
        ["uuid"],
        ondelete="CASCADE",
    )


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "pipeline_runs",
        sa.Column(
            "run_uuid", sa.VARCHAR(length=36), autoincrement=False, nullable=False
        ),
    )
    op.drop_constraint(
        op.f("fk_pipeline_runs_job_uuid_jobs"), "pipeline_runs", type_="foreignkey"
    )
    op.create_foreign_key(
        "fk_pipeline_runs_job_uuid_jobs",
        "pipeline_runs",
        "jobs",
        ["job_uuid"],
        ["job_uuid"],
        ondelete="CASCADE",
    )
    op.drop_column("pipeline_runs", "uuid")
    op.drop_constraint(
        op.f("fk_pipeline_run_steps_run_uuid_pipeline_runs"),
        "pipeline_run_steps",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "fk_pipeline_run_steps_run_uuid_pipeline_runs",
        "pipeline_run_steps",
        "pipeline_runs",
        ["run_uuid"],
        ["run_uuid"],
        ondelete="CASCADE",
    )
    op.drop_constraint(
        op.f("fk_pipeline_run_image_mappings_run_uuid_pipeline_runs"),
        "pipeline_run_image_mappings",
        type_="foreignkey",
    )
    op.create_foreign_key(
        "fk_pipeline_run_image_mappings_run_uuid_pipeline_runs",
        "pipeline_run_image_mappings",
        "pipeline_runs",
        ["run_uuid"],
        ["run_uuid"],
        ondelete="CASCADE",
    )
    op.add_column(
        "jobs",
        sa.Column(
            "job_name",
            sa.VARCHAR(length=255),
            server_default=sa.text("'job'::character varying"),
            autoincrement=False,
            nullable=False,
        ),
    )
    op.add_column(
        "jobs",
        sa.Column(
            "job_uuid", sa.VARCHAR(length=36), autoincrement=False, nullable=False
        ),
    )
    op.drop_column("jobs", "uuid")
    op.drop_column("jobs", "name")
    op.add_column(
        "environment_build",
        sa.Column(
            "build_uuid", sa.VARCHAR(length=36), autoincrement=False, nullable=False
        ),
    )
    op.drop_constraint(
        op.f("uq_environment_build_uuid"), "environment_build", type_="unique"
    )
    op.drop_column("environment_build", "uuid")
    # ### end Alembic commands ###
